<?php
/**
 * 
 * 商品信息
 * xysc users Version1.0
 * ==================================
 * Copy 2016-2017
 * Web:www.xjh7.com
 * ==================================
 * Author:xjh
 * Date:2016-3-5
 * model() 创建模型对象，静态
 * tableName() 返回表名
*/

class Goods extends CActiveRecord{
	
	public $page_list = array();//分页数组
	public $infos_count = 0;//统计查询个数
	/**
	 *返回当前模型对象的方法 
	 */
	public static function model($className = __CLASS__){
		return parent::model($className);
	}
	
	/**
	 * 返回当前表名
	 */
	public function tableName(){
		return '{{goods}}';
	}
	
	
	/**
	 * 根据分类id获取品牌
	 */
	public function getBrandByCategoryId($category_id){
		$all_model = new CCCModel();
		$_sql = "SELECT * FROM {{brand}} WHERE active = 1 AND  category_id=$category_id ORDER BY brand_sort ASC ";
		$result = $all_model->sel($_sql);
		return $result;
	}
	
	/**
	 * 标签列表
	 */
	public function showTag(){
		$all_model = new CCCModel();
		$_sql = "SELECT * FROM {{tag}}  ORDER BY tag_sort ASC ";
		$result = $all_model->sel($_sql);
		return $result;
	}
	
	
	/**
	 * 参数列表
	 */
	public function getParamInfo($goods_id){
		$cmodel = new CCCModel();
		$_sql = "SELECT a.param_name,(SELECT b.param_name FROM {{param}} b WHERE a.parent_id=b.param_id ) as parent_name  FROM {{param}} a WHERE a.parent_id <> 0 AND a.param_id IN(SELECT c.param_id FROM {{goods_param}} c WHERE c.goods_id=$goods_id AND c.active=1)";
		$result = $cmodel -> sel($_sql);
		return $result;
	}
	
	
	/**
	 * sku配置列
	 */
	public function getStyleInfo($goods_id){
		$cmodel = new CCCModel();
		$_sql = "SELECT a.attr_name,(SELECT b.attr_name FROM {{attr}} b WHERE a.parent_id=b.attr_id ) as parent_name  FROM {{attr}} a WHERE a.parent_id <> 0 AND a.attr_id IN(SELECT c.attr_id FROM {{goods_attr}} c WHERE c.goods_id=$goods_id AND c.active=1)";
		$result = $cmodel -> sel($_sql);
		return $result;
	}
	
	
	/**
	 * 根据商品查询店铺信息
	 */
	public function getGoodsStore($goods_id){
		$cmodel = new CCCModel();
		$_sql = "SELECT * FROM {{store}} WHERE store_id IN(SELECT store_id FROM {{goods_store}} WHERE goods_id=$goods_id) ";
		$result = $cmodel -> sel($_sql);
		return $result;
	}
	
	/**
	 * 获得推荐商品
	 */
	public function getRecGoods($store_id){
		$cmodel = new CCCModel();
		$_sql = "SELECT * FROM {{goods}} WHERE onit = 1 AND  goods_id IN(SELECT goods_id FROM {{goods_store}} WHERE rec=1 AND store_id=$store_id)";
		$result = $cmodel -> sel($_sql);
		return $result;
	}
	
	public function getHotBrandCover(){
		$cmodel = new CCCModel();
		$_sql = "SELECT * FROM {{brand}} WHERE brand_cover = 1 AND active = 1";
		$result = $cmodel -> sel($_sql);
		return $result;
	}
	
	/**
	 * 品牌汇总
	 */
	public function getAdverBrandList(){
		$cmodel = new CCCModel();
		$_sql = "SELECT * FROM {{adverbrand}}";
		$result = $cmodel -> sel($_sql);
		return $result;
	}
	
	
	
	
	
	public function getGoodsByGuess(){
		$cmodel = new CCCModel();
		$info = $cmodel->sel("SELECT * FROM {{goods}}");
		return $info;
	}
	
	/**
	 * 查询商品详细图
	 */
	public function getGoodsDetailsImg($goods_id){
		$cmodel = new CCCModel();
		$_sql = "SELECT * FROM {{images}} WHERE goods_id=".$goods_id;
		$result = $cmodel -> sel($_sql);
		return $result;
	}
	
	/**
	 * 查询热门品牌
	 */
	public function getHotBrand(){
		$cmodel = new CCCModel();
		$_sql = "SELECT * FROM {{brand}} WHERE active = 1 ORDER BY brand_sort LIMIT 0,24";
		$result = $cmodel -> sel($_sql);
		return $result;
	}
	

	/**
	 * 按照sort查询所有数据(8条),顶部导航
	 */
	public function all($n){
		$all_model = new CCCModel();
		$_sql = "SELECT nav_name,nav_url FROM {{nav}} WHERE nav_position = 1  ORDER BY nav_sort ASC LIMIT 0,$n";
		$result = $all_model->sel($_sql);
		return $result;
	}
	
	/**
	 * 按照sort查询所有数据,侧栏导航
	 */
	public function allSide(){
		$all_model = new CCCModel();
		$_sql = "SELECT nav_id,nav_name,nav_url FROM {{nav}} WHERE nav_position = 2  ORDER BY nav_sort ASC ";
		$result = $all_model->sel($_sql);
		return $result;
	}
	
	/**
	 * 获得最新上市的广告列表(推荐)
	 */
	public function getLastAdver(){
		$all_model = new CCCModel();
		$_sql = "SELECT adver_name,adver_thumbnail_url,adver_url FROM {{adver}} WHERE active = 1 AND adver_position = 1 AND adver_rec = 1";
		$result = $all_model->sel($_sql);
		return $result;
	}
	
	/**
	 * 获得最新上市的广告列表
	 */
	public function getLastAdverList(){
		$all_model = new CCCModel();
		$_sql = "SELECT adver_sort,adver_name,adver_thumbnail_url,adver_url FROM {{adver}} WHERE active = 1 AND adver_position = 1 AND adver_rec = 0 ORDER BY adver_sort";
		$result = $all_model->sel($_sql);
		return $result;
	}
	
	/**
	 * 获得品牌汇总广告列表
	 */
	public function getBrandAdverList(){
		$all_model = new CCCModel();
		$_sql = "SELECT adver_sort,adver_name,adver_thumbnail_url,adver_url FROM {{adver}} WHERE active = 1 AND adver_position = 3 AND adver_rec = 0 ORDER BY adver_sort LIMIT 0,4";
		$result = $all_model->sel($_sql);
		return $result;
	}
	
	
	/**
	 * 获得横条广告
	 */
	public function getHoradver(){
		$all_model = new CCCModel();
		$_sql = "SELECT adver_name,adver_thumbnail_url,adver_url FROM {{adver}} WHERE adver_position = 2  ORDER BY adver_sort";
		$result = $all_model->sel($_sql);
		return $result;
	}
	/**
	 * 根据id查找单条信息
	 */
	public function getGoodsById($goods_id){
		$cmodel = new CCCModel();
		$info = Yii::app()->cache->get('goods_info'.$goods_id);
		if(!empty($info))
			return $info;
		$_sql = "select * from {{goods}} where goods_id=".$goods_id;
		$result = $cmodel->sel($_sql);
		Yii::app()->cache->set('goods_info'.$goods_id,$result,3600);//缓存数据
		return $result;
	}
	
	/**
	 * 获得搜索分类
	 */
	
	public function getCategory(){
		$all_model = new CCCModel();
		$_sql = "SELECT * FROM {{category}}  ORDER BY sort ASC";
		$result = $all_model->sel($_sql);
		return $result;
	}
	
	/**
	 * 轮播图配置
	 */
	public function getTurnUrl($nums){
		$all_model = new CCCModel();
		$_sql = "SELECT turn_url FROM {{turn}} WHERE active = 1  ORDER BY turn_sort ASC  LIMIT 0,$nums";
		$result = $all_model->sel($_sql);
		return $result;
	}
	/**
	 * 评论读取
	 */
	public function getEvaContentByGoodsId($goods_id){
		$all_model = new CCCModel();
		$_sql = "SELECT a.content,b.user_name FROM {{eva_content}} a,{{user}} b WHERE a.user_id=b.user_id AND  goods_id=$goods_id";
		$result = $all_model->sel($_sql);
		return $result;
	}
	
	
	/**
	 * 搜索对应品牌下的商品
	 */
/**
	 * 分页+搜索
	 */
	public function pageSearch($brand_id = 0,$searchinfo = '',$kkeyword = ''){
		$_sql = '';
		$cmodel = new CCCModel();
		
		$_sql .= "SELECT * FROM {{goods}} WHERE 1=1 ";
		$nextpage = "";
		if ($brand_id != 0){     
			$_sql.=" AND brand_id=$brand_id";
			$_sql_count = str_replace('SELECT *','SELECT count(*)',$_sql);
		}
		if (@$searchinfo['send'] == '搜索'){
			$nextpage .= "&keyword=".$searchinfo['search'];
			$_sql.=" AND goods_name like '%".$searchinfo['search']."%' OR goods_info like '%".$searchinfo['search']."%'";
		}
		if (!empty($kkeyword)){
			$nextpage .= "&kkeyword='".$kkeyword."'";
			$_sql.=" AND goods_name like '%".$kkeyword."%' OR goods_info like '%".$kkeyword."%'";
		}
		if($brand_id == 0){
			$_sql_count = str_replace('SELECT *','SELECT count(*)',$_sql);
		}
		
	
			/**
			 * 分页
			 */
			$per = 8;
			$this->infos_count = $cmodel->sel($_sql_count);
			$cnt = $this->infos_count[0]['count(*)'];
			$page = new Pagination($cnt,$per,$nextpage);
			$_sql .= " ORDER BY goods_sort ASC $page->limit";
			$infos = $cmodel->sel($_sql);
			$page_list[0] = $page->fpage(array(3));
			$page_list[1] = $page->fpage(array(4));
			$page_list[2] = $page->fpage(array(5));
			$page_list[3] = $page->fpage(array(6));
			$page_list[4] = $page->fpage(array(7));
			
			$this->page_list[0] = $page_list[0];
			$this->page_list[1] = $page_list[1];
			$this->page_list[2] = $page_list[2];
			$this->page_list[3] = $page_list[3];
			$this->page_list[4] = $page_list[4];
			return $infos;
	}
	
	//根据店铺id与店名拼接
	function getStoreName(){
		$all_model = new CCCModel();
		$_sql = "SELECT store_id,store_name FROM {{store}} ";
		$result = $all_model->sel($_sql);
		return $result;		
	}
}
	
?>